package com.tust.controller;

import com.tust.domain.Course;
import com.tust.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
public class CourseController {
    private String userName;
    @Autowired
    private CourseService courseService;

    @ResponseBody
    @RequestMapping("/selectCourseAll.do")
    public List<Course> selectCourseAll(String userName) {
        List<Course> courses = courseService.selectCourseAll();
        this.userName = userName;
        return courses;
    }

    @ResponseBody
    @RequestMapping("/insertCourse.do")
    public String insertCourse(String courseNum, String courseName,
                               String credit, String openSemester,
                               String courseType, String classTimes,
                               String practiceTime, String regularGradeRate) {
        String msg =null;
        List<Course> course = courseService.selectCourseByCourseNum(courseNum);
        if (course.size() == 1) {//课程号存在
            msg = "courseNumExist";
        } else {
            Course newCourse = new Course(courseNum, courseName, credit,
                    openSemester, courseType, classTimes,
                    practiceTime, regularGradeRate);
            if (courseService.insertCourse(newCourse) == 1) {//插入成功
                msg = "insertSuccess";
            } else {//插入失败
                msg = "insertFalse";
            }
        }
        return msg;
    }

    @ResponseBody
    @RequestMapping("/updateCourse.do")
    public String updateCourse(String oldCourseNum, String courseNum,
                               String courseName, String credit,
                               String openSemester, String courseType,
                               String classTimes, String practiceTime,
                               String regularGradeRate) {
        String msg = null;
        List<Course> course = courseService.selectCourseByCourseNum(courseNum);
        if (course.size() == 1 && !oldCourseNum.equals(courseNum)) {//新课程号存在
            msg = "newCourseNumExist";
        } else {
            Course newCourse = new Course(courseNum, courseName, credit,
                    openSemester, courseType, classTimes,
                    practiceTime, regularGradeRate);
            if (courseService.updateCourse(oldCourseNum, newCourse) == 1) {//修改成功
                msg = "updateSuccess";
            } else {//修改失败
                msg = "updateFalse";
            }
        }
        return msg;
    }

    @ResponseBody
    @RequestMapping("/selectCourse.do")
    public List<Course> selectCourse(String courseNum, String courseName,
                                     String credit, String openSemester,
                                     String courseType, String classTimes,
                                     String practiceTime, String regularGradeRate) {
        List<Course> course = null;
        if (!courseNum.equals("")) {//查询课程号
            course = courseService.selectCourseByCourseNum(courseNum);
        } else if (!courseName.equals("")) {//查询课程名
            course = courseService.selectCourseByCourseName(courseName);
        } else if (!credit.equals("")) {//查询学分
            course = courseService.selectCourseByCredit(credit);
        } else if (!openSemester.equals("")) {//查询开课学期
            course = courseService.selectCourseByOpenSemester(openSemester);
        } else if (!courseType.equals("")) {//查询课程性质
            course = courseService.selectCourseByCourseType(courseType);
        } else if (!classTimes.equals("")) {//查询授课时数
            course = courseService.selectCourseByClassTimes(classTimes);
        } else if (!practiceTime.equals("")) {//查询实践时数
            course = courseService.selectCourseByPracticeTime(practiceTime);
        } else if (!regularGradeRate.equals("")) {//查询平时成绩比例
            course = courseService.selectCourseByRegularGradeRate(regularGradeRate);
        }
        return course;
    }

    @ResponseBody
    @RequestMapping("/deleteCourse.do")
    public String deleteCourse(String courseNum) {
        String msg = null;
        if (courseService.deleteCourse(courseNum) == 1) {//删除成功
            msg = "deleteSuccess";
        } else {//删除失败
            msg = "deleteFalse";
        }
        return msg;
    }

    @RequestMapping("/setCourseInfoRefresh.do")
    public ModelAndView setCourseInfoRefresh() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("setCourseInfo");
        mv.addObject("userName", userName);
        mv.addObject("modelName", "课程信息录入");
        mv.addObject("animate","false");
        return mv;
    }
}
